package upicgame.gamelibrary.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import upicgame.entity.Library;
import upicgame.entity.User;
import upicgame.gamelibrary.dao.LibraryDao;
@Repository
public class LibraryDaoImpl implements LibraryDao {

	@Autowired
	private SessionFactory factory;

	public Session getSession() {
		return factory.getCurrentSession();
	}
	//全查
	@Override
	public List<Library> findAllLibrary() {
		String hql = "from Library";
		List<Library> list = getSession().createQuery(hql,Library.class).getResultList();
		return list;
	}
	//用户id 查库
	@Override
	public List<Object[]> findLibraryByUserId(Integer uid) {
		String hql = "select id,l.user.id,l.user.uid,l.user.name,l.game.id,l.game.name from Library l where l.user.id=?";
		List<Object[]> obj  = getSession().createQuery(hql, Object[].class).setParameter(0, uid).getResultList();
		
		return obj;
	}
	//分页全查
	@Override
	public List<Library> findAllLibraryByPage(Integer currentPage, Integer maxSize) {
		String hql = "from Library";
		Query<Library> query = getSession().createQuery(hql, Library.class);
		query.setFirstResult((currentPage-1)*maxSize);
		query.setMaxResults(maxSize);
		List<Library> list = query.getResultList();
		return list;
	}
	//根据用户id 分页查
	@Override
	public List<Library> pageFindLibByUser(Integer uid, Integer currentPage, Integer maxSize) {
		String hql = "from Library l where l.user.id=?";
		Query<Library> query = getSession().createQuery(hql, Library.class);
		query.setParameter(0, uid);
		query.setFirstResult((currentPage-1)*maxSize);
		query.setMaxResults(maxSize);
		List<Library> list = query.getResultList();
		return list;
		
	}
	//计数
	@Override
	public Long countLibGame() {
		String hql = "select count(*) from Library";
		Long result = getSession().createQuery(hql,Long.class).getSingleResult();
		return result;
	}
	@Override
	public Long countLibGame(Integer uid) {
		String hql = "select count(*) from Library l where l.user.id=?";
		Long result = getSession().createQuery(hql,Long.class).setParameter(0, uid).getSingleResult();
		return result;
	}


	// 单查
	@Override
	public Library findlibByUserAndGame(Integer uid, Integer gid) {
		String hql = "from Library l where l.user.id=? and l.game.id=?";
		Library lib = getSession().createQuery(hql, Library.class).setParameter(0, uid).setParameter(1, gid)
				.uniqueResult();
		return lib;
	}

	// 增加
	@Override
	public void addGameInLibrary(Library lib) {
		getSession().save(lib);
	}

	// 单删
	@Override
	public Integer delGameInLibrary(Integer uid, Integer gid) {
		String hql = "delete from Library l where l.user.id=? and l.game.id=?";
		int result = getSession().createQuery(hql).setParameter(0, uid).setParameter(1, gid).executeUpdate();
		return result;

	}
	
	@Override
	public List<User> findAllUser() {
		String hql = "select new User(id,uid,name,email,money) from User";
		List<User> user = getSession().createQuery(hql,User.class).getResultList();
		return user;
	}

}
